<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" >添加角色</el-button>
        </el-col>
      </el-row>
      <el-table stripe border :data='rolesList' >
        <el-table-column type='expand'>
          <template  slot-scope='scope'>
            <el-row class='row' v-for='i in scope.row.children' :key='i.id'>
              <el-col :span='5'>
                <el-tag @close='delete3(scope.row, i.id)' closable>{{ i.authName }}</el-tag>
                <i class='el-icon-caret-right'></i>
              </el-col>
              <el-col :span='19' >
                <el-row class='top' v-for='j in i.children' :key='j.id'>
                  <el-col :span='6'>
                    <el-tag @close='delete3(scope.row, j.id)' closable type='success'>{{ j.authName }}</el-tag>
                    <i class='el-icon-caret-right'></i>
                  </el-col>
                  <el-col :span='18'>
                    <el-tag closable @close='delete3(scope.row, k.id)' type='warning' v-for='k in j.children' :key='k.id'>
                      {{ k.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type='index' ></el-table-column>
        <el-table-column label='角色名称' prop='roleName' ></el-table-column>
        <el-table-column label='角色描述' prop='roleDesc' ></el-table-column>
        <el-table-column label='操作' width='290px'>
          <template slot-scope='scope'>
            <el-button type="primary" size='mini' icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size='mini' icon="el-icon-delete">删除</el-button>
            <el-button @click='showSettingDialog(scope.row)' type="warning" size='mini' icon="el-icon-setting">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog @close='dialogClose' title="分配权限" :visible.sync="settingDialogVisi" width="50%">
      <el-tree ref='ref_tree' :data="allRolesList" :props="TreeProps" show-checkbox default-expand-all :default-checked-keys='defkey' node-key='id'></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="settingDialogVisi = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      rolesList:[],
      settingDialogVisi:false,
      allRolesList:[],
      TreeProps: {
        label: 'authName',
        children: 'children'
      },
      defkey:[],
      checkID:''
    }
  },
  methods:{
    async getData(){
      let res = await this.axios.get('/roles')
      if(res.data.meta.status == 200) {
        this.rolesList = res.data.data
      } else {
        this.pop.error( res.data.meta.msg )
      }
    },
    async delete3(row, rolesid){
      let res =await this.confirm('此操作将永久删除该权限', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch (err => {
        return err
      })
      if( res != 'confirm' ) {
        return this.pop('取消删除')
      }
      res = await this.axios.delete(`roles/${ row.id }/rights/${ rolesid }`)
      if(res.data.meta.status == 200) {
        this.pop.success(res.data.meta.msg)
        row.children = res.data.data
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    async showSettingDialog(row){
      this.checkID = row.id
      let res = await this.axios.get('rights/tree')
      if( res.data.meta.status == 200 ){
        this.digui(row)
        this.allRolesList = res.data.data
      }else{
        this.pop.error( res.data.meta.msg )
      }
      this.settingDialogVisi = true
    },
    digui( data ){
      if( !data.children ) {
        return this.defkey.push(data.id)
      } 
      data.children.forEach( item => {
        this.digui(item)
      })
    },
    dialogClose(){
      this.defkey = []
    },
    async submit(){
      let tree = this.$refs.ref_tree
      let keys = [...tree.getCheckedKeys(), ...tree.getHalfCheckedKeys()]
      let res = await this.axios.post(`roles/${ this.checkID }/rights`, { rids:keys.join(',') })
      if( res.data.meta.status == 200) {
        this.pop.success(res.data.meta.msg)
        this.getData()
        this.settingDialogVisi = false
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    
  },
  created(){
    this.getData()
  }
}
</script>

<style lang='scss' scoped>
.el-breadcrumb {
  padding:15px 0;
}
.el-tag {
  margin:10px 15px 10px 0;
}
.el-table {
  margin-top:15px;
}
.row {
  display: flex;
  justify-content: center;
  align-items: center;
  border-bottom:1px solid rgb(214, 214, 214);
}
.row:nth-of-type(1) {
  border-top:1px solid rgb(214, 214, 214);
}
.top {
  display: flex;
  justify-content: center;
  align-items: center;
  border-top:1px solid rgb(214, 214, 214);
}
.top:nth-of-type(1) {
  border-top:none;
}
</style>